//
//  detailTableViewCell.m
//  YouGoWeGo
//
//  Created by 范业君 on 14-11-20.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "detailTableViewCell.h"
#import "detaiTriplModel.h"
#import "UIImageView+WebCache.h"

@implementation detailTableViewCell

- (void)dealloc
{
    [_model release];
    [_addressLabel release];
    [_timeLabel release];
    [_bigImage release];
    [_introduce release];
    
    [self removeObserver:self forKeyPath:@"model"];
    
    [super dealloc];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.bigImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        //[_bigImage setBackgroundColor:[UIColor redColor]];
        [self.contentView addSubview:_bigImage];
        [_bigImage release];
        
        
        self.addressLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        //[_addressLabel setBackgroundColor:[UIColor redColor]];
        [_addressLabel setFont:[UIFont systemFontOfSize:13]];
        [self.contentView addSubview:_addressLabel];
        [_addressLabel release];
        
        
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        //[_timeLabel setBackgroundColor:[UIColor redColor]];
        [_timeLabel setFont:[UIFont systemFontOfSize:13]];
        [self.contentView addSubview:_timeLabel];
        [_timeLabel release];
        
        self.introduce = [[UILabel alloc] initWithFrame:CGRectZero];
        //[_introduce setBackgroundColor:[UIColor greenColor]];
        
        [_introduce setNumberOfLines:0];
        [_introduce setFont:[UIFont systemFontOfSize:10]];
        [self.contentView addSubview:_introduce];
        [_introduce release];
        
        [self addObserver:self forKeyPath:@"model" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"model"]) {
        [self.introduce setText:self.model.text];
        [self.bigImage setImageWithURL:[NSURL URLWithString:self.model.photo_s]];
        [self.timeLabel setText:self.model.local_time];
        [self.addressLabel setText:[self.model.poi objectForKey:@"name"]];
    }
}



- (void)layoutSubviews
{
    [super layoutSubviews];
   
    
    
    NSMutableDictionary *dic3 = self.model.photo_info;
    
    if (dic3 == nil) {
        [_bigImage setFrame:CGRectMake(0, 0, self.frame.size.width, 100)];
    } else {
        NSNumber *h = [dic3 objectForKey:@"h"];
        NSNumber *w = [dic3 objectForKey:@"w"];
        CGFloat h1 = [h floatValue];
        CGFloat w1 = [w floatValue];
        
        
        CGFloat height = self.frame.size.width * h1 / w1;
        [_bigImage setFrame:CGRectMake(10, 0, [UIScreen mainScreen].bounds.size.width - 20, height)];

    }
    
    
    
    
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12],NSFontAttributeName, nil];
    
        //根据文本内容大小（font）计算文本的rect值  最大不超过（375， 400）
    CGRect rect = [_introduce.text boundingRectWithSize:CGSizeMake(375, 60) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //[_detailLable setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, rect.size.height)];
    
    [_introduce setFrame:CGRectMake(_bigImage.frame.origin.x, _bigImage.frame.origin.y + _bigImage.frame.size.height + 5, _bigImage.frame.size.width, rect.size.height)];

   
    [_introduce sizeToFit];
    
    [_timeLabel setFrame:CGRectMake(_introduce.frame.origin.x, _introduce.frame.origin.y + _introduce.frame.size.height + 5, 130, 20)];
    
    [_addressLabel setFrame:CGRectMake(self.frame.size.width / 3 * 2, _timeLabel.frame.origin.y, 100, 20)];
    
    
}






- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
